package boogui.member;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import boogui.db.BooguiDAO;
import boogui.db.IBooguiDAO;
import boogui.db.MemberSQL;

/*
 * 아이디/비밀번호 찾기
 */

public class MemberFindDAO implements IMemberFindDAO {
	IBooguiDAO bdao = null;
	MemberSQL msql = null;

	public MemberFindDAO() {
		bdao = new BooguiDAO();
		msql = new MemberSQL();
	}

	// id찾기
	@Override
	public String findId(MemberVO m) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String result = "";

		try {
			conn = bdao.openDatabase();
			pstmt = conn.prepareStatement(msql.getFindIdSQL());
			pstmt.setString(1, m.getMemname());
			pstmt.setString(2, m.getMememail());
			rs = pstmt.executeQuery();
			if (rs.next()) {
				result = rs.getString("memid");
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			bdao.closeDatabase(conn, pstmt, rs);
		}
		return result;
	}

	// 비밀번호 찾기
	@Override
	public String findPwd(MemberVO m) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String result = "";

		try {
			conn = bdao.openDatabase();
			pstmt = conn.prepareStatement(msql.getFindPwdSQL());
			pstmt.setString(1, m.getMemid());
			pstmt.setString(2, m.getMemname());
			pstmt.setString(3, m.getMememail());
			rs = pstmt.executeQuery();
			if (rs.next()) {
				result = rs.getString("mempwd");
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			bdao.closeDatabase(conn, pstmt, rs);
		}
		return result;
	}

	@Override
	public boolean changePwd(String id, String pwd) {
		Connection conn = null;
		PreparedStatement pstmt = null;		
		boolean result = false;

		try {
			conn = bdao.openDatabase();
			pstmt = conn.prepareStatement(msql.getChangePwdSQL());
			pstmt.setString(1, pwd);
			pstmt.setString(2, id);
			pstmt.executeUpdate();
			
			result = true;			
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			bdao.closeDatabase(conn, pstmt, null);
		}
		
		return result;
		
	}
}
